<?php 
class annonce_creer extends MX_Controller{
	public $category;
	public function index(){
		

		$loadTarif =$this->load->Module('setting/tarif');
		$tarif=$loadTarif->get_tarif();
		
		
		
		$title="Vous déposez annonce ".$this->input->post('tilte');
		$description="Téléphones portables, écrans plats LED et LCD, ordinateurs et jeux vidéo, appareil photos, achetez et vendez d’occasion sur Caverne";
		$keywords = preg_split("/[\s,]+/", $this->input->post('tilte'));
		$keywords = implode(',', $keywords);
		
		
		$product=new stdclass;
		$this->load->helper( 'text' );
		$title_annonce=convert_accented_characters($this->input->post('tilte'));
		
		$this->load->helper('url');
		$product->prd_name= $this->input->post('tilte');
		
		$product->prd_price=str_replace(array(',',' '),array('.',''),$this->input->post('Prix'));
		$product->prd_description=strip_tags($this->input->post('description'), '<p><blockquote><ul><ol><li><strong><b><u><i><h1><h2><h3><h4><h5><br><hr><span>');
		$product->prd_tag=$keywords;
		$product->prd_keywords=$keywords;
		$product->category_id=$this->input->post('category');
		
		$product->prd_type=$this->input->post('type');
		$product->prd_link=url_title($title_annonce);
		$product->prd_codepostal=$this->input->post('code_postal');
		$product->prd_departement= $this->input->post('departement');
		if($product->prd_departement=='' or $product->prd_departement==0)$product->prd_departement=substr($product->prd_codepostal, 0, 2);
		
		$product->date_validate=$product->date_create=strtotime('now');
		$product->option=$this->input->post('option');
		$product->prd_youtube= $this->input->post('youtube');
		
		$product->company= $this->input->post('company');
		$product->user_name= $this->input->post('nom');
		$product->user_email= $this->input->post('email');
		$product->user_tel= $this->input->post('telephone');
		$product->user_tel_show= $this->input->post('tel_show');
		$product->shop_id= $this->input->post('shop_id');
		$product->user_password= $this->input->post('password');
		$product->user_ip=$this->input->ip_address();
		 
		$photos=$this->input->post('photo');
		//deplacer les photo dans bon dossier categories
		$product->photo=self::copy_Photo(FCPATH."images/",$product->category_id,$photos);
		$productInfo= $this->load->Module('product/show');
		
		$product->prd_active=1;//have to 0 when we finish test
		
		if(self::valide($product)){
		
			$message='';
			$id=$this->model()->create($product);
			
			$product=$productInfo->get_info($id,false);
			
			//partie Indexation
			
			$indexation=$this->load->module("indexation/admin");
			
			$indexation->product_info_id=$id;
			//1 point pour description
			$indexation->chaine=$product->prd_description;
			$indexation->poids=1;
			$indexation->save();
			//2 point pour le tile
			$indexation->chaine=$product->prd_name;
			$indexation->poids=2;
			$indexation->save();
			
			//3 point pour le mots clé
			$indexation->chaine=$product->prd_keywords;
			$indexation->poids=3;
			$indexation->save(); 
			$showTariff=true;
			
			//send email  have to turn on when finish test
			
			$email= $this->load->Module('send/email');
			//load template email 
			$setting=$this->load->Module('setting/admin');
			$from_email=$setting->get_admin('admin_email');
			
			$action='confirm_annonce';
			$edition=$this->load->Module('setting/edition');
			$edition_send=$edition->get_Edition($action);
			$from_name='Caverne.fr';
			$to_email=$product->user_email;
			//si le subject est vide il prend le 1eme line dans theme d'édition
			$subject="";
			$message=$edition_send[0]->setting;
			$message_data=(array)$product;
			$message_data['from_email']=$from_email;
			$message_data['tilte']=$product->prd_name;
			foreach ($message_data as $key=>$data){
				if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
			}
			$status='send';
			$description='';
				
			$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
			

			//$message= "Un email de confirmation vient de vous être envoyé <br/> Pour valider votre annonce, cliquez sur le lien présent dans l'email. Merci de votre confiance!";
			
			$message= "Votre annonce a bien été envoyé. Nous vous remercions de nous avoir confiance";
		}else{
			$product_doublon=$this->model()->get_doublon($product);
			$product=$productInfo->get_info($product_doublon->prd_id,false);
			$message= "Votre annonce n'est pas valide, ou vous avez posté cet annonce, 
					retounez <a href='{$this->config->config['base_url']}annonce/deposer'>l'étape précendant</a>";
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'message'=>$message,
				'annonce'=>$product,		
				'filter'=>'',
				'tarif'=>$tarif
		);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		if(isset($showTariff))
			$this->view(
					array('header',
						'annonce-creer',
						'annonce-tarif',
						'footer'), 
					$data); // Display the page
		else{
			$this->view(
					array('header',
							'annonce-creer',
									'footer'),
					$data); // Display the page
		}
	}
	private function valide($product){
		if(is_object($product) and 
			isset($product->user_email) and $product->user_email!='' and 
			isset($product->prd_name) and $product->prd_name!='' and 
			isset($product->category_id) and is_numeric($product->category_id) ){
			return $this->model()->check_doublon($product);
		}
		return false;
	}
	
	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo=$path.$photo;
			$temp_thumb_photo=$path."thumbnails/".$photo;
			$NameCatPhoto=$cat."/$photo";
	
			if(!is_dir($path.$cat))mkdir($path.$cat, 0777);
			if(!is_dir($path."thumbnails/".$cat))mkdir($path."thumbnails/".$cat, 0777);
	
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,$path."thumbnails/$NameCatPhoto")) unlink($temp_thumb_photo);
	
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
}